#ifndef __CE_BUF_H__
#define __CE_BUF_H__

#include "ce_core.h"

typedef struct ce_chain_s ce_chain_t;

typedef struct {
    char           *pos;
    char           *last;

    char           *start;
    char           *end;

    char            data[];
} ce_buf_t;

struct ce_chain_s {
    ce_chain_t     *next;
    ce_chain_t     *tail;

    unsigned        ref;

    // ce_buf_t       *buf;
    ce_buf_t        in_buf;
};

#define ce_buf_size(buf) (size_t) ((buf)->last - (buf)->pos)
#define ce_buf_cap(buf)  (size_t) ((buf)->end  - (buf)->start)

void ce_chain_pool_init(size_t size);

ce_chain_t *ce_chain_get(ce_chain_t **head, ce_buf_t *buf);
ce_chain_t *ce_chain_put(ce_chain_t *ch);
void ce_chain_acquire(ce_chain_t *ch);
void ce_chain_put_all(ce_chain_t *head);

#define ce_chain_buf(ch) &((ch)->in_buf)

#endif
